import axios from "axios";
import { utils } from "@/js/utils";
import qs from "qs";
// import https from "https";

export class Ajax {
    constructor(baseUrl) {
        // axios.defaults.withCredentials = true;
        //添加一个请求拦截器
        // axios.interceptors.request.use(function (config) {
        //     let satoken = localStorage.getItem('satoken')
        //     config.headers.common['satoken'] = satoken;
        //     //console.dir(config);
        //     return config;
        // }, function (error) {
        //     // Do something with request error
        //     console.info("error: ");
        //     console.info(error);
        //     return Promise.reject(error);
        // });
        // 单例,为了解决session丢失的问题,降低了一些几率而已
        // https.globalAgent.options.rejectUnauthorized = false;
        // const agent = new https.Agent({
        //     rejectUnauthorized: false,
        //     ca: 'yang wang'
        // });
        if (!this.instance) {
            // console.log("ajax instance constructor.")
            Ajax.prototype.instance = axios.create({
                baseURL: baseUrl,
                timeout: 20000,
                // withCredentials: true,
                headers: {
                    'Access-Control-Allow-Credentials': 'true',
                    'Access-Control-Allow-Origin': "*"
                    // 'Access-Control-Allow-Origin': baseUrl
                },
                // httpsAgent: agent,
                // httpsAgent: new https.Agent({
                //     rejectUnauthorized: false,
                //     ca: 'yang wang'
                // })
            });
        }

        // Ajax.prototype.instance.onRequest(config => {
        //     if (process.env.dev) {
        //         config.httpsAgent = agent;
        //     }
        // });

        this.ut = new utils();
    }

    /**
     * UrlencodedForm 方式上传, webflux需要使用exchange从form中查询
     * @param {*} url 上传路径
     * @param {*} obj 需要自己构建{key:value,}
     */
    postUrlencodedForm(url, obj) {
        let phone = this.ut.getValue("phone");
        let accessKey = this.ut.getValue("accessKey");
        let token = this.ut.getValue("X-Admin-Token");
        console.log("token", token);
        console.log(this.ut.getValue("token_time"));
        let headers = { phone: phone, accessKey: accessKey, 'X-Admin-Token': token, 'Content-Type': 'application/x-www-form-urlencoded' };

        return this.instance.post(url,
            qs.stringify(obj),
            { headers: headers }
        );
    }

    /**
     * 
     * @param {*} url 
     * @param {*} jsondata 
     */
    postJson(url, jsondata) {
        // let phone = this.ut.getValue("phone");
        // let accessKey = this.ut.getValue("accessKey");
        // let token = this.ut.getValue("X-Admin-Token");
        // console.log("token", token);
        // console.log(this.ut.getValue("token_time"));
        // let headers = {
        //     phone: phone,
        //     accessKey: accessKey,
        //     'X-Admin-Token': token,
        //     'Content-Type': 'application/json'application/x-www-form-urlencoded;
        // };
        // console.log(headers)
        let satoken = localStorage.getItem('satoken');
        return this.instance.post(url,
            jsondata,
            {
                headers: { satoken: satoken },
                // httpsAgent: new https.Agent({
                //     rejectUnauthorized: false
                // })
            }
        );
    }

    postJsonWithHeader(url, data, satoken) {
        return this.instance.post(url,
            data,
            {
                headers: { satoken },
            }
        );
    }

    /**
     * 
     * @param {*} url 
     * @param {*} formdata 
     */
    postMultipartForm(url, formdata, satoken) {
        return this.instance.post(url,
            formdata,
            { headers: { "Content-Type": "multipart/form-data", "satoken": satoken } }
        );
    }

    postForm(url, formdata, satoken) {
        return this.instance.post(url,
            formdata,
            { headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', satoken } }
        );
    }

    get(url, callback) {
        if (!callback) {
            this.instance.get(url).then(res => callback(res));
            return;
        }
        return this.instance.get(url);
    }

    post(url, data) {
        return this.instance.post(url, data);
    }

    postFile(url, file) {
        let phone = this.ut.getValue("phone");
        let accessKey = this.ut.getValue("accessKey");
        let formdata = new FormData();
        formdata.append("file", file, file.name);
        formdata.append("contentType", file.type);
        return this.instance.post(url,
            formdata,
            { headers: { "Content-Type": "multipart/form-data", phone: phone, accessKey: accessKey } }
        );
    }

}

export default Ajax